var fs = require('fs');
module.exports = function(app){

app.get('/app/createUser', function(req, res) {
	res.render('create_user');
});

app.post('/app/createUser', function(req, res) {
			
	var user = new AV.User();
	
	user.set("username", req.param('username'));
	user.set("password", req.param('password'));
	user.set("email", req.param('email'));	
	user.set("usertype", req.param('usertype'));
	user.set("usable", req.param('usable'));
	user.set("nickname", req.param('nickname'));
	user.set("phone", req.param('phone'));

	var photoFile = req.files.photoFile;
	if(photoFile.size > 0){		
		fs.readFile(photoFile.path, function(err, data){
		  if(err){return res.send("读取文件失败");}
		  var base64Data = data.toString('base64');
		  var avFile = new AV.File(photoFile.name, {base64: base64Data});
		  avFile.save().then(function(avFile){
			user.set("photoFile", avFile);
			user.signUp(null, {
			  success: function(user) {
				res.render('success', { message: '保存成功！' });
			  },
			  error: function(user, error) {
				res.render('error', { message: error.message });
				console.log('signin false: %j', error.code +" : "+ error.message);
			  }
			});					
		  });
		});			
	
	} else {
		user.signUp(null, {
		  success: function(user) {
				res.render('success', { message: '保存成功！' });
		  },
		  error: function(user, error) {
			res.render('error', { message: error.message });
			console.log('signin false: %j', error.code +" : "+ error.message);
		  }
		});	
	}
		
	
});

app.get('/app/openUser', function(req, res) {	
	var userObj = AV.Object.extend("User");
	var query = new AV.Query(userObj);
	query.include("wealth");
	query.include("photoFile");
	query.get(req.param("userId"), {
		success: function(userObj) {
			var wealth = userObj.get("wealth");
			var photoFile = userObj.get("photoFile");			
			res.render('update_user', { PhotoFile: photoFile,User: userObj, UserWealth: wealth });					  
		},
		error: function(userObj, error) {
			res.render('error', { message: error.message });
			console.log('signin false: %j', error.code +" : "+ error.message);
		}
	});			
	
});

app.get('/app/userList', function(req, res) {
	var page = 0;
	if(req.param("page") != undefined){
		page = parseInt(req.param("page"));
	}
	var nextPage = page + 1;
	var previousPage = page - 1;
	var skip = page * 10;
	
	var User = AV.Object.extend("User");
	var query = new AV.Query(User);
	query.containedIn("usertype",["0", "1"]);
	query.skip(skip);
	query.limit(10);
	query.find({
		success: function(results) {
			if(results.length > 0){
				res.render('user_list', { Users: results, next: nextPage, previous: previousPage});
			} else {
				res.render('user_list', { Users: results, next: 0, previous: previousPage});
			}
			
		},
		error: function(error) {
			res.render('error', { message: error.message });
			console.log('signin false: %j', error.code +" : "+ error.message);	
		}
	});			
});

app.post('/app/updateUser', function(req, res) {
	
	var photoFile = req.files.photoFile;
	if(photoFile.size > 0){		
		fs.readFile(photoFile.path, function(err, data){
		  if(err){return res.send("读取文件失败");}
		  var base64Data = data.toString('base64');
		  var avFile = new AV.File(photoFile.name, {base64: base64Data});
		  avFile.save().then(function(avFile){
				var params = {

				userId:req.param("userId"),
				
				/*username:req.param("username"),
				password:req.param("password"),
				email:req.param("email"),
				usertype:req.param("usertype"),
				usable:req.param("usable"),
				nickname:req.param("nickname"),
				realname:req.param("realname"),
				phone:req.param("phone"),
				signature:req.param("signature"),
				
				oldPhotoFileId:req.param("oldPhotoFileId"),
				newPhotoFileId:avFile.id
				*/
				};
				
				//console.log('params == : %j', params);
				AV.Cloud.run("updateUser", params, {
				  success: function(data){
					  res.render('success', { message: '修改成功！' });
				  },
				  error: function(error){
					 res.render('error', { message: '修改失败！'});
					 console.log('update false: %j', error.code +" : "+ error.message);
				  }
				});
		  });
		});			
	
	} else {
		var params = {

		userId:req.param("userId"),
		
		/*username:req.param("username"),
		password:req.param("password"),
		email:req.param("email"),
		usertype:req.param("usertype"),
		usable:req.param("usable"),
		nickname:req.param("nickname"),
		realname:req.param("realname"),
		phone:req.param("phone"),
		signature:req.param("signature"),
		*/
		};
		
		//console.log('params == : %j', params);
		AV.Cloud.run("updateUser", params, {
		  success: function(data){
			  res.render('success', { message: '修改成功！' });
		  },
		  error: function(error){
			 res.render('error', { message: '修改失败！'});
			 console.log('update false: %j', error.code +" : "+ error.message);
		  }
		});	
	}	
});

app.post('/app/deleteUser', function(req, res) {
   
	var user = AV.Object.createWithoutData("User", req.param('userId'));
	user.destroy({
		success: function(user) {
			res.send('success');
		},
		error: function(user, error) {
			res.render('error', { message: error.message });
			console.log('signin false: %j', error.code +" : "+ error.message);				
		}
	});	
	
});
};